//
//  BRBaseViewController.m
//  BrezzeSaaS
//
//  Created by IOS on 2021/8/31.
//  Copyright © 2021 Brezze GWG. All rights reserved.
//

#import "BRBaseViewController.h"

@interface BRBaseViewController ()<QMUICustomNavigationBarTransitionDelegate>
{
    QMUILabel *_titleL;
}

@end

@implementation BRBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor whiteColor];
//    NSLog(@"%@",USERManager.userModel.token);
    self.view.backgroundColor = kColorBGF5;
//    self.qmui_preferredStatusBarStyleBlock = ^UIStatusBarStyle{
//        return UIStatusBarStyleDefault;
//    };
//    self.qmui_previousViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:UIImageMake(@"arrow_back_dark") style:UIBarButtonItemStylePlain target:nil action:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    
}

#pragma mark - Action --
- (void)popVC:(BOOL)animated {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dismissVC:(BOOL)animated {
    [self dismissViewControllerAnimated:animated completion:nil];
}

- (void)pop {
    [self popVC:YES];
}

- (void)dismiss {
    [self dismissVC:YES];
}

- (void)showBREmptyView {
    [self showBREmptyView:self.tableView];
}

- (void)showBREmptyView:(UIScrollView *)aView {
    aView.ly_emptyView = self.emptyLYView;
    [aView ly_endLoading];
}

- (void)showBRErrorView {
    [self showBRErrorView:self.tableView];
}
- (void)showBRErrorView:(UIScrollView *)aView {
    aView.ly_emptyView = self.netErrorView;
    [aView ly_endLoading];
}

- (void)br_blockAction {}
- (void)br_addNoti{}

#pragma mark - Set UI --
- (void)setUI {
    NSLog(@"setUI");
}

- (UIView *)createTop:(NSString *)title {
    return [self createTop:title imageStr:@"arrow_left_gary44"];
}

- (UIView *)createTop:(NSString *)title backColor:(UIColor *)color {
    return [self createTop:title imageStr:@"arrow_left_gary44" backColor:color];
}

/// 无背景导航，需隐藏原本导航栏preferredNavigationBarHidden
- (UIView *)createTop:(NSString *)title imageStr:(NSString *)imgStr {
    return [self createTop:title imageStr:imgStr backColor:UIColorClear];
}

/// 无背景导航，需隐藏原本导航栏preferredNavigationBarHidden
- (UIView *)createTop:(NSString *)title imageStr:(NSString *)imgStr backColor:(UIColor *)color {
    
    UIView *top = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kSafeAreaTopHeight)];
    top.backgroundColor = color;
    [self.view addSubview:top];
    
    QMUIButton *backBtn = [[QMUIButton alloc] initWithFrame:CGRectMake(12, KStatusBarHeight, 44, 44)];
    [backBtn setImage:UIImageMake(imgStr) forState:UIControlStateNormal];
    [top addSubview:backBtn];
    [backBtn addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
    if (imgStr.length == 0) {
        backBtn.hidden = YES;
    }
    if (title.length != 0) {
        QMUILabel *titleLbel = [[QMUILabel alloc] init];
        titleLbel.text  = title;
        titleLbel.textColor = UIColor.qd_backgroundColor;
        titleLbel.font = [UIFont br_fontB:17];
        [top addSubview:titleLbel];
        [titleLbel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(58);
            make.centerY.equalTo(backBtn);
        }];
        _titleL = titleLbel;
    }
    return top;
}

- (BRNavView *)br_createTop:(NSString *)title {
    return [self br_createTop:title imageStr:@"arrow_left_LBlue44"];
}

- (BRNavView *)br_createTop:(NSString *)title backColor:(UIColor *)color {
    return [self br_createTop:title imageStr:@"arrow_left_LBlue44" backColor:color];
}

/// 无背景导航，需隐藏原本导航栏preferredNavigationBarHidden
- (BRNavView *)br_createTop:(NSString *)title imageStr:(NSString *)imgStr {
    return [self br_createTop:title imageStr:imgStr backColor:UIColorClear];
}

/// 无背景导航，需隐藏原本导航栏preferredNavigationBarHidden
- (BRNavView *)br_createTop:(NSString *)title imageStr:(NSString *)imgStr backColor:(UIColor *)color {
    
    return [self br_createTop:title imageStr:imgStr imageRStr:@"" backColor:color];
}

- (BRNavView *)br_createTop:(NSString *)title imageRStr:(NSString *)rImage {
    return [self br_createTop:title imageStr:@"arrow_left_LBlue44" imageRStr:rImage backColor:UIColorClear];
}

- (BRNavView *)br_createTop:(NSString *)title imageStr:(NSString *)imgStr imageRStr:(NSString *)rImage  backColor:(UIColor *)color {
    
    BRNavView *top = [[BRNavView alloc] init];
    top.frame = CGRectMake(0, 0, kScreenWidth, kSafeAreaTopHeight + 8);
    top.backgroundColor = color;
    [self.view addSubview:top];
    
    if (imgStr.length != 0) {
        [top br_addBtnleft:imgStr];
    }
    if (rImage.length != 0) {
        [top br_addBtnRight:rImage];
    }
    if (title.length != 0) {
        top.titleL.text = title;
    }
    _navView = top;
    WS(weakSelf);
    top.leftClickBlock = ^{
        if (weakSelf.leftClickBlock) {
            weakSelf.leftClickBlock();
            return;
        }
        [weakSelf pop];
    };
    top.rightClickBlock = ^{
        if (weakSelf.rightClickBlock) weakSelf.rightClickBlock();
    };
    
    return top;
}

- (void)setTitleColor:(UIColor *)titleColor {
    _titleColor = titleColor;
    if (_titleL) {
        _titleL.textColor = titleColor;
    }
    if (_navView) {
        _navView.titleL.textColor = titleColor;
    }
}

- (void)setTitleStr:(NSString *)titleStr {
    _titleStr = titleStr;
    if (_titleL) {
        _titleL.text = titleStr;
    }
    if (_navView) {
        _navView.titleL.text = titleStr;
    }
}

- (void)tableViewAdjustmentNever {
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

/// 控制导航栏是否显示
- (BOOL)shouldCustomizeNavigationBarTransitionIfHideable {
    return YES;
}
/// 默认显示导航栏
- (BOOL)preferredNavigationBarHidden {
    return YES;
}

- (UIImage *)navigationBarBackgroundImage {
    return [UIImage qmui_imageWithColor:UIColor.qd_backgroundColor];
}

- (void)dealloc {
    NSLog(@"-- %@ ->销毁",NSStringFromClass([self class]));
}

- (QMUITableView *)tableView{
    if (!_tableView) {
        UITableViewStyle styyle = self.tableType == 1 ? UITableViewStyleGrouped : UITableViewStylePlain;
        _tableView = [[QMUITableView alloc]initWithFrame:CGRectMake(0,kSafeAreaTopHeight, kScreenWidth, kScreenHeight - kSafeAreaTopHeight) style:styyle];
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }

        if (@available(iOS 15.0, *)) {
            [UITableView appearance].sectionHeaderTopPadding = 0;
        }
        if (@available(iOS 11.0, *)) {
            self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    }
    return _tableView;
}

- (BREmptyView *)emptyLYView {
    if (!_emptyLYView) {
        _emptyLYView = [[BREmptyView alloc] initEmptyViw];
    }
    
    return _emptyLYView;
}

- (BREmptyView *)netErrorView {
    if (!_netErrorView) {
        _netErrorView = [[BREmptyView alloc] initErrorViw];
    }
    return _netErrorView;
}

/**
 往类上添加新的方法与其实现
 
 @param class 相应的类
 @param methodSel 添加的方法
 @param methodSelImpl 包含方法实现的SEL
 */
+ (void)addMethod:(Class)class method:(SEL)methodSel method:(SEL)methodSelImpl {
    Method method = class_getInstanceMethod(class, methodSelImpl);
    IMP methodIMP = method_getImplementation(method);
    const char *types = method_getTypeEncoding(method);
    class_addMethod(class, methodSel, methodIMP, types);
}

+ (void)load {

    [self addMethod:self.class method:@selector(forceEnableInteractivePopGestureRecognizer) method:@selector(br_forceEnableInteractivePopGestureRecognizer)];
    
    
}


#pragma mark - 下拉刷新、加载更多
- (MJRefreshStateHeader *)addRefreshActionForView:(UIScrollView *)aView {
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(handleListRefreshAction)];
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    header.automaticallyChangeAlpha = YES;
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    aView.mj_header = header;
    
    return header;
}

- (MJRefreshFooter *)addLoadMoreActionForView:(UIScrollView *)aView {
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadMoreData方法）
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(handleListLoadMoreAction)];
    aView.mj_footer = footer;
    return footer;
}

- (void)handleListRefreshAction {
    
}

- (void)handleListLoadMoreAction {
    
}

- (void)handleEndRefreshForView:(UIScrollView *)aView {
    [aView.mj_header endRefreshing];
    [aView.mj_footer endRefreshing];
}

- (void)handleEndRefreshForViewWithNoMoreData:(UIScrollView *)aView
{
    [aView.mj_footer endRefreshingWithNoMoreData];
}

- (BOOL)br_forceEnableInteractivePopGestureRecognizer {

    
    return YES;

}

//- (UIStatusBarStyle)qmui_preferredStatusBarStyle {
//    return UIStatusBarStyleDefault;
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
